<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/5/31
 * Time: 21:37
 */
namespace App\Service\Goods;

use App\Models\Goods\GoodsCommentModel;
use App\Service\Customer\CustomerService;
use Illuminate\Support\Facades\DB;

class GoodsCommentService
{

    /**
     * 获得单条评论
     */
    public static function getOneComment($goods_id)
    {
        $comments_info = GoodsCommentModel::where('goods_id',$goods_id)->orderBy("created_at","desc")->limit(1)->first();

        $user_info = CustomerService::findUser(['uid'=>$comments_info['uid']]);

        $result = array(
            'headimgurl' => $user_info['headimgurl'],
            'nickname' => $user_info['nickname'],
            'uid' => $comments_info['uid'],
            'content' => $comments_info['content'],
            'created_at' => $comments_info['created_at'],
            'comment_num' => GoodsCommentModel::where('goods_id',$goods_id)->count(),
        );
        return $result;
    }

    /**
     *  获得详情页评论
     */
    public static function getMoreComments($goods_id)
    {
        $comments_info = GoodsCommentModel::where('goods_id',$goods_id)->orderBy("created_at","desc")->limit(4)->get();

        $result = [];
        if($comments_info->isEmpty()){
            return $result;
        }

        foreach($comments_info as $comment){
            $user_info = CustomerService::findUser(['uid'=>$comment['uid']]);

            $result[] = array(
                'headimgurl' => $user_info['headimgurl'],
                'nickname' => $user_info['nickname'],
                'uid' => $comment['uid'],
                'content' => $comment['content'],
                'created_at' => $comment['created_at']
            );
        }
        return $result;
    }

    /**
     *  获得评论数量
     */
    public static function getCommentsNum($goods_id)
    {
        return GoodsCommentModel::where('goods_id',$goods_id)->count();
    }

    /**
     * 获得评论列表
     */
    public static function getCommentList($goods_id,$offset)
    {
        $comments_info = DB::table('lsj_goods_comment_log as comment')
            ->join('lsj_user as user','comment.uid','=','user.uid')
            ->where('goods_id',$goods_id)
            ->select('comment.uid','comment.content','comment.created_at','comment.photos','user.headimgurl','user.nickname')
            ->limit(10)
            ->offset($offset)
            ->get();

        return $comments_info;
    }

    /**
     * 评价商品
     */
    public static function commentGoods($data=[])
    {
        return DB::table('lsj_goods_comment_log')->insert($data);
    }
}